package com.etlmaster.executor.parser;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.etlmaster.executor.Job;

/**
 * 将xml对象解析成dp job对象
 * 
 * @author Wugf
 * @date 2016-03-08
 */
public abstract class XmlJobParser {

	protected XmlJob xmlJob= new XmlJob();;
	public Map<String,Object> jobParam;
	protected Map<String,Object> params;
	public List<Map<String,Object>> rowList;
	private void parseJobInfo(){
		xmlJob.jobId = (String)jobParam.get("job_id");
		xmlJob.jobName = (String)jobParam.get("job_name");
		
		if(params!=null && params.size()>0){
			xmlJob.jobBatchNo=(String)params.get("jobBatchNo");
			
			if(xmlJob.jobBatchNo==null || xmlJob.jobBatchNo.length()<=0)
				xmlJob.jobBatchNo = (String)jobParam.get("job_batch_no");
		}
		
		xmlJob.teamCode = (String)jobParam.get("team_code");
		xmlJob.step = new ArrayList<XmlStep>();
	}
	private void parseStepInfo(){
		for (int i = 0;rowList !=null && i < rowList.size(); i++) {
			Map<String,Object> row = rowList.get(i);
			XmlStep stepXml = new XmlStep();
			stepXml.id= String.valueOf(row.get("step_id"));
			stepXml.name= (String)row.get("step_name");
			stepXml.completeSteps = String.valueOf(row.get("n_step"));
			stepXml.successSteps= String.valueOf(row.get("s_step"));//不能保证每步都执行成功，故添加成功时、失败时、完成时二元判断
			stepXml.failedSteps= String.valueOf(row.get("f_step"));
			stepXml.nextSteps = (String)row.get("next_steps_id");
			
			stepXml.stepInst = (String)row.get("step_inst");
			stepXml.value = (String)row.get("parameters");
			xmlJob.step.add(stepXml);
		}
	}
	public abstract void init(String jobId,Map<String,Object> params);
	
	public abstract Job builder(String jobId, Map<String, Object> params);
	
	public abstract List<Map<String, Object>> findSenderStepCfg(String stepId);
	
	public XmlJob load(String jobId,Map<String,Object> params){

		this.params=params;
		init(jobId,params);
		parseJobInfo();
		parseStepInfo();
		return xmlJob;
	}

}
